package com.wxg.demo;

import java.io.File;

import com.wxg.entity.MyPages;
import com.wxg.print.BuildFinalPrintPage;
import com.wxg.print.StoreHtmlWithGenId;
import com.wxg.print.WhosOverPage;
import com.wxg.utils.MyDriver;

public class Emr104 {
	
	/**
	 * 添加id后存储到本地的html文件的地址
	 */
	private String genFilepath;
	
	/**
	 * 每页应该输出的block元素的id集合
	 */
	private MyPages pages;
	
	/**
	 * 如果源是一个html文件,将使用此变量存储
	 */
	private String sourceHtmlpath;
	
	/**
	 * 源是一个html字符串
	 * 这种情况更符合正式项目的逻辑(源html从百度UE发送过来)
	 */
	private String sourceHtmlStr;
	
	public static enum HtmlSourceType {
		HtmlFile, HtmlStr
	}
	private HtmlSourceType htmlSourceType;
	
	private String idPrefix; // id前缀(id_)
	
	/**
	 * 增加一个source属性
	 * (sourceHtmlpath, sourceHtmlStr, sourceFile)
	 * 
	 */
	private File sourceFile;
	
	public Emr104(String source, HtmlSourceType type){
		this.htmlSourceType = type;
		if(type == HtmlSourceType.HtmlFile){
			sourceHtmlpath = source;
		}
		if(type == HtmlSourceType.HtmlStr){
			sourceHtmlStr = source;
		}
	}
	
	public Emr104(File file){
		this.sourceFile = file;
	}
	
	public String getIdPrefix() {
		return idPrefix;
	}
	
	public String genHtml(){
		
		StoreHtmlWithGenId storeHtmlWithGenId = new StoreHtmlWithGenId();
		if(null != sourceFile){
			genFilepath = storeHtmlWithGenId.store(sourceFile);
		}else if(htmlSourceType == HtmlSourceType.HtmlFile){
			genFilepath = storeHtmlWithGenId.store(new File(sourceHtmlpath));
		}else{
			genFilepath = storeHtmlWithGenId.store(sourceHtmlStr);
		}
		
		idPrefix = storeHtmlWithGenId.getPrefix();
		
		WhosOverPage whosOverPage = new WhosOverPage(genFilepath, idPrefix);
		whosOverPage.parse();
		
		pages = whosOverPage.getPages();
		
		BuildFinalPrintPage bfpp = new BuildFinalPrintPage(genFilepath, pages);
		return bfpp.finalPrintPage();
		
		// whosOverPage(); // 在`whosOverPage()`内部调用`getBlockEleBounds()`
//		return insertHeaderAndFooter();
	}
	
	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Emr104 emr = null;
		System.out.println( System.currentTimeMillis() );
		String dirpath = "F:\\wxg\\for_print\\myjfserver\\src\\main\\webapp\\uehtml";
		File dir = new File(dirpath);
		String[] allowArr = "html_103,html_104,html_105,html_109,html_110,html_114,html_115,html_116,html_117,html_118,html_120".split(",");
		
		for(File file : dir.listFiles()){
			//if(My.in(file.getName().replace(".html", ""), allowArr)){
			if( file.getName().contains("html_120") ){
				emr = new Emr104(file);
				System.out.println( emr.genHtml() );
				System.out.println( System.currentTimeMillis() );
			}
		}
		// String filepath = dirpath + "\\html_102.html";
		MyDriver.closeStaticDriver();
	}
	
}
